package com.cqxyd.smartsite.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cqxyd.smartsite.pojo.DeviceInfo;
import com.cqxyd.smartsite.pojo.DeviceInfoVo;
import com.cqxyd.smartsite.service.DeviceInfoService;
import com.cqxyd.smartsite.util.KeyValueUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import springfox.documentation.annotations.ApiIgnore;

@Controller
@ApiIgnore
@RequestMapping(value = "/deviceInfo")
public class DeviceInfoController {
    @Autowired
    private DeviceInfoService deviceInfoService;

    // [start] 查询
    @RequestMapping(value = "/list")
    public String list(Model model,String programId,String programName) {
        model.addAttribute("programId",programId);
        model.addAttribute("programName",programName);
        model.addAttribute("deviceTypes", KeyValueUtil.getDeviceTypes());
        return "deviceInfo/deviceInfo-list";
    }

    @RequestMapping(value = "/deviceInfoList")
    @ResponseBody
    public String deviceInfoList(Integer pageNum, Integer pageSize, Integer programId, String seriaNo, String registNo, String typeId)
            throws Exception {
        PageInfo<DeviceInfoVo> pageInfo = deviceInfoService.getDeviceInfoListByPage(pageNum, pageSize, programId, seriaNo, registNo, typeId);
        JSONObject result = new JSONObject();
        result.put("rows", pageInfo.getList());
        result.put("total", pageInfo.getTotal());
        return result.toJSONString();
    }
    // [end]

    // [start] 新增

    @RequestMapping(value = "/add")
    public String insert(Model model, Integer programId,String programName) {
        model.addAttribute("programId",programId);
        model.addAttribute("programName",programName);
        model.addAttribute("deviceTypes", KeyValueUtil.getDeviceTypes());
        return "deviceInfo/deviceInfo-add";
    }

    @ResponseBody
    @RequestMapping(value = "/insert")
    public String insert(HttpServletRequest request, HttpServletResponse response, DeviceInfo deviceInfo) {
        try {
            deviceInfo.setCreateTime(new Date());
            deviceInfoService.addDeviceInfo(deviceInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redireck:deviceInfo/deviceInfo-list";
    }

    // [end]

    // [start] 更新

    @RequestMapping(value = "/edit")
    public String edit(Model model, @RequestParam("deviceId") Integer deviceId,
                       @RequestParam("programName") String programName) {
        try {
            model.addAttribute("programName", programName);
            model.addAttribute("deviceTypes", KeyValueUtil.getDeviceTypes());
            DeviceInfo deviceInfo = deviceInfoService.findDeviceInfo(deviceId);
            model.addAttribute("currentDeviceInfo", deviceInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "deviceInfo/deviceInfo-edit";
    }

    @RequestMapping(value = "/updateSave")
    @ResponseBody
    public String updateSave(DeviceInfo currentDeviceInfo) {
        try {
            int result = deviceInfoService.updateDeviceInfo(currentDeviceInfo);
            if (result != 1) {
                return "{\"success\":false}";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "{\"success\":true}";
    }

    // [end]

    // [start] 删除

    @RequestMapping(value = "/delete")
    @ResponseBody
    public String delete(@RequestParam("deviceId") Integer deviceId) {
        try {
            int result = deviceInfoService.deleteDeviceInfo(deviceId);
            if (result != 1) {
                return "{\"success\":false}";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "{\"success\":false}";
        }
        return "{\"success\":true}";
    }
}
